package com.linwen.excise.leetcode;

/**
 * @ClassName _704二分查找
 * @Description 二分查找
 * @Author lin
 * @Date 2021/11/7 下午12:32
 * @Version V1.0
 */
public class _704二分查找 {
    public static void main(String[] args) {
        int[] nums = new int[]{5};
        int target = 5;
        System.out.println(search(nums,target));
    }
    public static int search(int[] nums, int target) {
        int start = 0;
        int end = nums.length-1;
        int mid = (nums.length-1)/2;
        while(start<=end){
            if(target>nums[mid]){
                start = mid+1;
                mid = (start+end)/2;
            }else if(target<nums[mid]){
                end = mid - 1;
                mid = (start+end)/2;
            }else if(target == nums[mid]){
                return mid;
            }
        }
        return -1;
    }
}
